package com.loubobooo.personal.blog.controller.backend;

import com.loubobooo.personal.blog.entity.Notice;
import com.loubobooo.personal.blog.entity.vo.NoticeVO;
import com.loubobooo.personal.blog.service.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;
import java.util.List;

@Controller @RequestMapping("/admin/notice") public class NoticeManageController {

    @Autowired private NoticeService noticeService;

    //后台公告列表显示
    @RequestMapping(value = "") public ModelAndView index() {
        ModelAndView modelandview = new ModelAndView();

        List<NoticeVO> noticeVOList = noticeService.listNotice(null);
        modelandview.addObject("noticeVOList", noticeVOList);

        modelandview.setViewName("Admin/Notice/index");
        return modelandview;

    }

    //添加公告显示
    @RequestMapping(value = "/insert") public ModelAndView insertNoticeView() {
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("Admin/Notice/insert");
        return modelAndView;
    }

    //添加公告提交
    @RequestMapping(value = "/insertSubmit", method = RequestMethod.POST) public String insertNoticeSubmit(
            Notice notice) {
        notice.setNoticeCreateTime(new Date());
        notice.setNoticeUpdateTime(new Date());
        notice.setNoticeStatus(1);
        notice.setNoticeOrder(1);
        noticeService.insertNotice(notice);
        return "redirect:/admin/notice";
    }

    //删除公告
    @RequestMapping(value = "/delete/{id}") public String deleteNotice(@PathVariable("id") Integer id) {
        noticeService.deleteNotice(id);

        return "redirect:/admin/notice";
    }

    //编辑公告页面显示
    @RequestMapping(value = "/edit/{id}") public ModelAndView editNoticeView(@PathVariable("id") Integer id) {
        ModelAndView modelAndView = new ModelAndView();

        NoticeVO noticeVO = noticeService.getNoticeById(id);
        modelAndView.addObject("noticeVO", noticeVO);

        modelAndView.setViewName("Admin/Notice/edit");
        return modelAndView;
    }

    //编辑公告提交
    @RequestMapping(value = "/editSubmit", method = RequestMethod.POST) public String editNoticeSubmit(Notice notice) {
        notice.setNoticeUpdateTime(new Date());
        noticeService.updateNotice(notice);
        return "redirect:/admin/notice";
    }

}
